/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing.workers;

import cz.muni.fi.pv168.uloha3.common.DBUtils;
import cz.muni.fi.pv168.uloha3.entities.Owner;
import cz.muni.fi.pv168.uloha3.entities.Ownership;
import cz.muni.fi.pv168.uloha3.entities.RealEstate;
import cz.muni.fi.pv168.uloha3.exceptions.OwnerManagerException;
import cz.muni.fi.pv168.uloha3.managers.OwnerManager;
import cz.muni.fi.pv168.uloha3.managers.OwnershipManager;
import cz.muni.fi.pv168.uloha3.managers.RealEstateManager;
import cz.muni.fi.pv168.uloha3.managers.impl.OwnerManagerImpl;
import cz.muni.fi.pv168.uloha3.managers.impl.OwnershipManagerImpl;
import cz.muni.fi.pv168.uloha3.managers.impl.RealEstateManagerImpl;
import cz.muni.fi.pv168.uloha3.swing.OwnersTableModel;
import cz.muni.fi.pv168.uloha3.swing.SwingGuiMain;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.swing.SwingWorker;

/**
 *
 * @author stefan
 */
public class OwnerListAllSwingWorker extends SwingWorker<Set<Owner>, Object> {

    private final OwnersTableModel model;
    private final RealEstate excludeForRealEstate;
    

    public OwnerListAllSwingWorker(OwnersTableModel model) {
        this.excludeForRealEstate = null;
        this.model = model;
    }
    
    public OwnerListAllSwingWorker(RealEstate excludeForRealEstate, OwnersTableModel model) {
        this.excludeForRealEstate = excludeForRealEstate;
        this.model = model;
    }
    
    @Override
    protected Set<Owner> doInBackground() throws Exception {
        
        Set<Owner> retval = new HashSet<Owner>();
        
        DataSource dataSource = DBUtils.initConnector();
        
        OwnerManager managerOwner = new OwnerManagerImpl();
        managerOwner.setDataSource(dataSource);
        
        model.clearAllOwners();

        try {
            retval.addAll(managerOwner.getOwners());
            
            if(excludeForRealEstate != null) {
                RealEstateManager managerRealEstate = new RealEstateManagerImpl();
                managerRealEstate.setDataSource(dataSource);
                OwnershipManager managerOwnership = new OwnershipManagerImpl();
                managerOwnership.setDataSource(dataSource);
                managerOwnership.setOwnerManager(managerOwner);
                managerOwnership.setRealEstateManager(managerRealEstate);
                for(Ownership ownership : managerOwnership.getOwnershipsByRealEstate(excludeForRealEstate)) {
                    retval.remove(ownership.getOwner());
                }
            }
        } catch (OwnerManagerException ex) {
            Logger.getLogger(SwingGuiMain.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        model.addAllOwners(retval);
        return retval;
    }
}
